package com.demo.activiti.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ActivitiService {
	
	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private TaskService taskService;
	
	/**
	 * 流程发起
	 * @param personID
	 * @param companyID
	 */
	public void startProcess(Long personID,Long companyID){
		Map<String,Object> variables = new HashMap<String, Object>();
		variables.put("personID" , personID);
		variables.put("companyID", companyID);
		runtimeService.startProcessInstanceByKey("joinProcess",variables);
	}
	
	/**
	 * 获取某个用户的任务列表
	 * @param assignee
	 * @return
	 */
	public List<Task> getTasks(String assignee){
		List<Task> tasks = taskService.createTaskQuery().taskCandidateUser(assignee).list();
		return tasks.size()>0?tasks:null;
	}
	
	/**
	 * 完成任务
	 * @param joinApproved
	 * @param taskID
	 */
	public void completeTask(Boolean joinApproved, String taskID){
		Map<String,Object> variables = new HashMap<String, Object>();
		variables.put("joinApproved" , joinApproved);
		taskService.complete(taskID, variables);
	}
	

}
